3 * Base class for the output of file transformation methods.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Media transform output for images
29 class ThumbnailImage
extends MediaTransformOutput
{
30 private static $firstNonIconImageRendered = false;
33 * Get a thumbnail object from a file and parameters.
34 * If $path is set to null, the output file is treated as a source copy.
35 * If $path is set to false, no output file will be created.
36 * $parameters should include, as a minimum, (file) 'width' and 'height'.
37 * It may also include a 'page' parameter for multipage files.
40 * @param string $url URL path to the thumb
41 * @param string|bool $path Filesystem path to the thumb
42 * @param array $parameters Associative array of parameters
44 function __construct( $file, $url, $path = false, $parameters = [] ) {
45 # Previous parameters:
46 # $file, $url, $width, $height, $path = false, $page = false
53 if ( is_array( $parameters ) ) {
54 $actualParams = $parameters +
$defaults;
56 # Using old format, should convert. Later a warning could be added here.
57 $numArgs = func_num_args();
60 'height' => $parameters,
61 'page' => ( $numArgs > 5 ) ?
func_get_arg( 5 ) : false
63 $path = ( $numArgs > 4 ) ?
func_get_arg( 4 ) : false;
70 # These should be integers when they get here.
71 # If not, there's a bug somewhere. But let's at
72 # least produce valid HTML code regardless.
73 $this->width
= round( $actualParams['width'] );
74 $this->height
= round( $actualParams['height'] );
76 $this->page
= $actualParams['page'];
77 $this->lang
= $actualParams['lang'];
81 * Return HTML <img ... /> tag for the thumbnail, will include
82 * width and height attributes and a blank alt text (as required).
84 * @param array $options Associative array of options. Boolean options
85 * should be indicated with a value of true for true, and false or
88 * alt HTML alt attribute
89 * title HTML title attribute
90 * desc-link Boolean, show a description link
91 * file-link Boolean, show a file download link
92 * valign vertical-align property, if the output is an inline element
93 * img-class Class applied to the \<img\> tag, if there is such a tag
94 * desc-query String, description link query params
95 * override-width Override width attribute. Should generally not set
96 * override-height Override height attribute. Should generally not set
97 * no-dimensions Boolean, skip width and height attributes (useful if
99 * custom-url-link Custom URL to link to
100 * custom-title-link Custom Title object to link to
101 * custom target-link Value of the target attribute, for custom-target-link
102 * parser-extlink-* Attributes added by parser for external links:
103 * parser-extlink-rel: add rel="nofollow"
104 * parser-extlink-target: link target, but overridden by custom-target-link
106 * For images, desc-link and file-link are implemented as a click-through. For
107 * sounds and videos, they may be displayed in other ways.
109 * @throws MWException
112 function toHtml( $options = [] ) {
113 global $wgPriorityHints, $wgElementTiming;
115 if ( func_num_args() == 2 ) {
116 throw new MWException( __METHOD__
. ' called in the old style' );
119 $alt = $options['alt'] ??
'';
121 $query = $options['desc-query'] ??
'';
126 'decoding' => 'async',
129 $elementTimingName = 'thumbnail';
131 if ( $wgPriorityHints
132 && !self
::$firstNonIconImageRendered
133 && $this->width
* $this->height
> 100 * 100 ) {
134 self
::$firstNonIconImageRendered = true;
136 $attribs['importance'] = 'high';
137 $elementTimingName = 'thumbnail-high';
140 if ( $wgElementTiming ) {
141 $attribs['elementtiming'] = $elementTimingName;
144 if ( !empty( $options['custom-url-link'] ) ) {
145 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
146 if ( !empty( $options['title'] ) ) {
147 $linkAttribs['title'] = $options['title'];
149 if ( !empty( $options['custom-target-link'] ) ) {
150 $linkAttribs['target'] = $options['custom-target-link'];
151 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
152 $linkAttribs['target'] = $options['parser-extlink-target'];
154 if ( !empty( $options['parser-extlink-rel'] ) ) {
155 $linkAttribs['rel'] = $options['parser-extlink-rel'];
157 } elseif ( !empty( $options['custom-title-link'] ) ) {
158 /** @var Title $title */
159 $title = $options['custom-title-link'];
161 'href' => $title->getLinkURL(),
162 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
164 } elseif ( !empty( $options['desc-link'] ) ) {
165 $linkAttribs = $this->getDescLinkAttribs(
166 empty( $options['title'] ) ?
null : $options['title'],
169 } elseif ( !empty( $options['file-link'] ) ) {
170 $linkAttribs = [ 'href' => $this->file
->getUrl() ];
172 $linkAttribs = false;
173 if ( !empty( $options['title'] ) ) {
174 $attribs['title'] = $options['title'];
178 if ( empty( $options['no-dimensions'] ) ) {
179 $attribs['width'] = $this->width
;
180 $attribs['height'] = $this->height
;
182 if ( !empty( $options['valign'] ) ) {
183 $attribs['style'] = "vertical-align: {$options['valign']}";
185 if ( !empty( $options['img-class'] ) ) {
186 $attribs['class'] = $options['img-class'];
188 if ( isset( $options['override-height'] ) ) {
189 $attribs['height'] = $options['override-height'];
191 if ( isset( $options['override-width'] ) ) {
192 $attribs['width'] = $options['override-width'];
195 // Additional densities for responsive images, if specified.
196 // If any of these urls is the same as src url, it'll be excluded.
197 $responsiveUrls = array_diff( $this->responsiveUrls
, [ $this->url
] );
198 if ( !empty( $responsiveUrls ) ) {
199 $attribs['srcset'] = Html
::srcSet( $responsiveUrls );
202 Hooks
::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
204 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );